---
layout: "default"
title: "Comparable"
description: "Swift documentation for 'Comparable': Instances of conforming types can be compared using relational."
keywords: "Comparable,protocol,swift,documentation,<,<=,==,>,>="
root: "/v2.2"
---

<div class="intro-declaration"><code class="language-swift">protocol Comparable</code></div>

<div class="discussion comment">
    <p>Instances of conforming types can be compared using relational
operators, which define a <a href="http://en.wikipedia.org/wiki/Total_order#Strict_total_order">strict total order</a>.</p>

<p>A type conforming to <code>Comparable</code> need only supply the <code>&lt;</code> and
<code>==</code> operators; default implementations of <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code>, and
<code>!=</code> are supplied by the standard library:</p>

<pre><code class="language-swift">struct Singular : Comparable {}
func ==(x: Singular, y: Singular) -&gt; Bool { return true }
func &lt;(x: Singular, y: Singular) -&gt; Bool { return false }</code></pre>

<p><strong>Axioms</strong>, in addition to those of <code>Equatable</code>:</p>

<ul><li><code>x == y</code> implies <code>x &lt;= y</code>, <code>x &gt;= y</code>, <code>!(x &lt; y)</code>, and <code>!(x &gt; y)</code></li><li><code>x &lt; y</code> implies <code>x &lt;= y</code> and <code>y &gt; x</code></li><li><code>x &gt; y</code> implies <code>x &gt;= y</code> and <code>y &lt; x</code></li><li><code>x &lt;= y</code> implies <code>y &gt;= x</code></li><li><code>x &gt;= y</code> implies <code>y &lt;= x</code></li></ul>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Equatable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>







<h3>Instance Methods</h3>
<div class="declaration" id="func-lt_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-lt_rhs_">func &lt;(<wbr>_:<wbr>rhs:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-lt_rhs_"><div class="p">
    <p>A <a href="http://en.wikipedia.org/wiki/Total_order#Strict_total_order">strict total order</a>
over instances of <code>Self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func &lt;(lhs: Self, rhs: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-lteq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-lteq_rhs_">func &lt;=(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func-lteq_rhs_"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">func &lt;=(lhs: Self, rhs: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration inherited" id="func-eqeq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-eqeq_rhs_">func ==(<wbr>_:<wbr>rhs:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-eqeq_rhs_"><div class="p">
    <p>Returns <code>true</code> if <code>lhs</code> is equal to <code>rhs</code>.</p>

<p><strong>Equality implies substitutability</strong>.  When <code>x == y</code>, <code>x</code> and
<code>y</code> are interchangeable in any code that only depends on their
values.</p>

<p>Class instance identity as distinguished by triple-equals <code>===</code>
is notably not part of an instance&#39;s value.  Exposing other
non-value aspects of <code>Equatable</code> types is discouraged, and any
that <em>are</em> exposed should be explicitly pointed out in
documentation.</p>

<p><strong>Equality is an equivalence relation</strong></p>

<ul><li><code>x == x</code> is <code>true</code></li><li><code>x == y</code> implies <code>y == x</code></li><li><code>x == y</code> and <code>y == z</code> implies <code>x == z</code></li></ul>

<p><strong>Inequality is the inverse of equality</strong>, i.e. <code>!(x == y)</code> iff
<code>x != y</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func ==(lhs: Self, rhs: Self) -&gt; Bool</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/Equatable/"><code>Equatable</code></a>    
</div></div>
</div>
<div class="declaration" id="func-gt_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-gt_rhs_">func &gt;(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func-gt_rhs_"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">func &gt;(lhs: Self, rhs: Self) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-gteq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-gteq_rhs_">func &gt;=(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func-gteq_rhs_"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">func &gt;=(lhs: Self, rhs: Self) -&gt; Bool</code>
    
    
</div></div>
</div>


